package com.chance.cc.crawler.prod.command.job.domain.vm.autohome.adhoc;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRecord;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.vm.autohome.AutoHomeCommonCrawlerSchedulerJob;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;
import static com.chance.cc.crawler.development.scripts.allfeild.AICCommonField.Tag_Site_Info;

/**
 * @author lt
 * @version 1.0
 * @date 2021-07-06 09:58:40
 * @email okprog@sina.com
 */
public class AutoHomeForumArticleSimpleCrawlerSchedulerJob extends AutoHomeCommonCrawlerSchedulerJob {
    private static final String crawler_level = "simple_reply";
    private static final String site = "forum";
    private static final String siteBiz = "forum";

    private static final String OrderType = "order_type";//回复排序 1  发布排序 2

    public static void main(String[] args) {

        publishAutoHomeCrawlerScheduleJobInfo();

    }

    /**
     * 发布任务信息
     * @return
     */
    public static CrawlerJob publishAutoHomeCrawlerScheduleJobInfo(){
        CrawlerJob autoHomeScheduler = autoHomeScheduler();

        //发布定时任务
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(autoHomeScheduler));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("汽车之家论坛帖子临时采集");
        crawlerScheduleJob.setCrawlerKey(autoHomeScheduler.generateCrawlerKey());
        HttpPage httpPage = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布任务：" + httpPage.getRawText());
        return autoHomeScheduler;
    }
    public static CrawlerJob autoHomeScheduler(){
        //论坛-采集
        CrawlerRequestRecord forumCrawler = doForumCrawler();

        CrawlerJob crawlerJob = CrawlerJob.builder()
                .triggerInfo(domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Simple,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-", site,"article", crawler_level, CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-", "crawler", domain, site, crawler_level, "queue")))
                .kafkaResultPipeline(null, kafkaTopciForTraceJob, null)
                .fileResultPipeline(null, "/data/chance_crawler_runner/logs/node/autohome/forum_article_simple.log", false)
                .crawlerJobThreadNumber(20)
                .requestRecord(forumCrawler)
                .build();
        //添加评论去重信息
        CrawlerRecord filterCrawlerRecord = new CrawlerRecord();
        filterCrawlerRecord.setFilter(CrawlerEnum.CrawlerRecordFilter.dateRange);
        filterCrawlerRecord.addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-",filter,domain,"comment")));
        filterCrawlerRecord.addFilterInfo(FilterUtils.dateRangeFilterInfo(0,new long[]{1630425600000L,System.currentTimeMillis()}));
        forumCrawler.tagsCreator().bizTags().addCustomKV("comment_record_filter_info", JSON.toJSONString(filterCrawlerRecord));
        crawlerJob.getScheduleTags().getCategoryTag().addLabelTag(CrawlerEnum.CrawlerDataType.comment.enumVal());//采集文章回复（若不采集：注掉）
        return crawlerJob;

    }

    public static CrawlerRequestRecord doForumCrawler(){

        String url = "http://club.autohome.com.cn/bbs/thread/ff1a385d089fb9ca/99439785-1.html";
        HttpConfig httpConfig = HttpConfig.me(domain);
        httpConfig.setHttpSite(site);

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .httpUrl(url)
                .releaseTime(System.currentTimeMillis())
                .httpConfig(httpConfig)
                .domain(domain)
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(0,new long[]{1630425600000L,System.currentTimeMillis()}))
                .needParsed(false)
                .proxy(proxy)
                .build();
        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);

        requestRecord.tagsCreator().bizTags().addDomain(domain);
        requestRecord.tagsCreator().bizTags().addSite(site);
        requestRecord.tagsCreator().bizTags().addSiteBiz(siteBiz);
        requestRecord.tagsCreator().bizTags().addCustomKV(Tag_Site_Info,"forum-article-20211101");
        requestRecord.tagsCreator().bizTags().addCustomKV(OrderType,"1");
        Map<String,Object> extras = new HashMap<>();
        extras.put("path","");
        requestRecord.getHttpRequest().setExtras(extras);
        return requestRecord;
    }
}
